import { _decorator, Component, Node, resources, ProgressBar, director, Label, Color } from 'cc'
import { SCENE_ENUM } from '../../Enum'
const { ccclass, property } = _decorator

/**
 * Predefined variables
 * Name = LoadingManager
 * DateTime = Tue Aug 02 2022 09:46:49 GMT+0800 (中国标准时间)
 * Author = Zero_Y
 * FileBasename = LoadingManager.ts
 * FileBasenameNoExtension = LoadingManager
 * URL = db://assets/Scripts/Scene/LoadingManager.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */

@ccclass('LoadingManager')
export class LoadingManager extends Component {
  @property(ProgressBar)
  bar: ProgressBar = null
  onLoad() {
    resources.preloadDir(
      'texture',
      (cur, total) => {
        const p = cur / total
        this.bar.progress = p
        const lab = this.node.getComponentInChildren(Label)
        lab.string = '游戏资源加载中...' + (p * 100).toFixed(2) + '%'
      },
      () => {
        // 加载完成
        director.loadScene(SCENE_ENUM.Start)
      },
    )
  }
}
